package dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao.FoodDao;
import dao.RowMapper;
import entity.Food;
import entity.FoodType;

public class FoodDaoImpl implements FoodDao{

	JDBCTemplate jdbc =new JDBCTemplate();
	
	private class rowMapperImpl implements RowMapper<Food>{
		@Override
		public Food rowMapper(ResultSet rs) throws SQLException {
			FoodType foodtype = new FoodType();
			foodtype.setFtId(rs.getInt("foodTypeId"));
			foodtype.setFoodTypeName(rs.getString("foodTypeName"));
			
			Food food = new Food();
			food.setfId(rs.getInt("fId"));
			food.setFoodType(foodtype);
			food.setFoodName(rs.getString("foodname"));
			food.setFoodprice(rs.getDouble("Foodprice"));
			food.setImageFile(rs.getString("imageFile"));
			food.setDescription(rs.getString("description"));
			
			return food;
			
		}
	}
	
	@Override
	public void addFood(Food food) {
		String sql = "INSERT INTO food(foodTypeId,foodName,foodprice,description,imageFile) VALUES(?,?,?,?,?)";
		jdbc.CUDObject(sql, food.getFoodType().getFtId(),
				food.getFoodName(),food.getFoodprice(),
				food.getDescription(),
				food.getImageFile());
		
	}

	@Override
	public void updateFood(Food food) {
		String sql = "UPDATE food SET foodTypeId=?,foodName=?,foodprice=?,description=?,imageFile=? WHERE fid=?";
		jdbc.CUDObject(sql, food.getFoodType().getFtId(),
				food.getFoodName(),
				food.getFoodprice(),
				food.getDescription(),
				food.getImageFile(),
				food.getfId());
		
	}

	@Override
	public void deleteFood(int foodId) {
		String sql = "DELETE FROM food WHERE fid=?";
		jdbc.CUDObject(sql, foodId);		
	}

	@Override
	public List<Food> queryFood(List<Food> foodList,int pageNum,int pageSize) {
		List<Food> list = jdbc.queryByPaging(foodList, pageNum, pageSize);
		
		return list;
	}
	
	
	
	@Override
	public List<Food> queryAllFood() {
		String sql = "SELECT food.*,foodType.`foodTypeName` "
				+ "FROM food INNER JOIN foodtype ON food.`foodTypeId`=foodtype.`ftId` ";
		return jdbc.queryAll(sql, new rowMapperImpl());
	}

	@Override
	public List<Food> queryFoodByTypeId(int foodTypeId) {
		return query("foodTypeId", foodTypeId);
	}

	@Override
	public Food queryFoodByFoodId(int foodId) {
		return query("foodId", foodId).get(0);
	}

	@Override
	public Integer queryFoodTotalCount(int foodTypeId) {
		String sql ="SELECT count(*) FROM food where foodTypeId = ?";
		return jdbc.queryOne(sql, new RowMapper<Integer>() {

			@Override
			public Integer rowMapper(ResultSet rs) throws SQLException {
				return rs.getInt(1);
			}
		}, foodTypeId);
	}
	@Override
	public Integer queryAllFoodTotalCount() {
		String sql ="SELECT count(*) FROM food";
		return jdbc.queryOne(sql, new RowMapper<Integer>() {

			@Override
			public Integer rowMapper(ResultSet rs) throws SQLException {
				return rs.getInt(1);
			}
		});
	}
	
	private List<Food> query(String type,Integer id){
		List<Food> list = new ArrayList<Food>();
		String sql = "SELECT food.*,foodType.`foodTypeName` "
				+ "FROM food INNER JOIN foodtype ON food.`foodTypeId`=foodtype.`ftId` ";
		if(id!=null){
			if ("foodTypeId".equals(type)) {
				sql+=" where foodTypeId=? ";
			}else if("foodId".equals(type)){
				sql+=" where fid=? ";
			}
			list=jdbc.queryAll(sql, new rowMapperImpl(), id);
		}else{
			list=jdbc.queryAll(sql, new rowMapperImpl());
		}

		return list!=null&&list.size()>0?list:null;
	}
	



}
